using System;
using System.Collections.Generic;
using System.Linq;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using Aphysoft.Vate;
using Aphysoft.Vate.Graphics;

namespace Sample
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Sample : Vate
    {
        public Sample() : base("Vate Sample Game", DefinitionSize.HD)
        {
            StartUpWidth = 800;
            StartUpHeight = 600;
            StartUpAspectRatio = ScreenRatio.FourToThree;

            TargetFps = 800;
        }

        private Primitive cp;
        private Transformation tr;
        private Camera dc;
        private MouseTrackingCameraController mc;
        private Transformation tr2, tr3, tr4, tr5, tr6, tr7, tr8;
        private float trspeedinms = 0.002f;

        protected override void GameInitialize()
        {            
        }
            
        protected override void GameLoadContent()
        {
            dc = new Camera("test");

            ProjectorManager.DefaultProjector.Camera = dc;

            mc = new MouseTrackingCameraController();
            mc.Camera = dc;
            mc.HorizontalNegativeTrackLimit = 0.3f;
            mc.HorizontalPositiveTrackLimit = 0.6f;

            dc.Position = new Vector3(0, 0, 5);

            dc.DefineCameraUp = true;
            dc.DefineCameraTarget = false;

            cp = new TeapotPrimitive(Device);
            
            tr = new Transformation(true);

            tr2 = new Transformation() { Position = new Vector3(5, 0, 0) };
            tr3 = new Transformation() { Position = new Vector3(-5, 0, 0) };
            tr4 = new Transformation() { Position = new Vector3(0, 5, 0) };
            tr5 = new Transformation() { Position = new Vector3(0, 5, 5) };
            tr6 = new Transformation() { Position = new Vector3(5, 0, 5) };
            tr7 = new Transformation() { Position = new Vector3(0, 0, 10) };
            tr8 = new Transformation() { Position = new Vector3(5, 0, 10) };
        }

        protected override void GameUnloadContent()
        {
            cp.Dispose();            
        }        

        protected override void GameUpdate(GameTime gameTime)
        { 
            if (Vate.InputReady)
            {
                if (IsKeyDown(KeyboardKeys.E))
                    tr.RotateAroundYaw(-trspeedinms * TimeDelta);
                else if (IsKeyDown(KeyboardKeys.Q))
                    tr.RotateAroundYaw(trspeedinms * TimeDelta);

                if (IsKeyDown(KeyboardKeys.D))
                    tr.RotateAroundRoll(trspeedinms * TimeDelta);
                else if (IsKeyDown(KeyboardKeys.A))
                    tr.RotateAroundRoll(-trspeedinms * TimeDelta);

                if (IsKeyDown(KeyboardKeys.W))
                    tr.RotateAroundPitch(-trspeedinms * TimeDelta);
                else if (IsKeyDown(KeyboardKeys.S))
                    tr.RotateAroundPitch(trspeedinms * TimeDelta);

                if (IsKeyDown(KeyboardKeys.Y))
                    tr.Rotate(new Vector3(2, 2, 0), new Vector3(0, 1, 0), trspeedinms * TimeDelta * 5);

                if (IsStepLeftPressed())
                    mc.Reset();
                else if (IsLeftPressed())
                    mc.Track(3);
               
                if (IsKeyDown(KeyboardKeys.Z))
                    dc.TranslateForward(0.02f);
                else if (IsKeyDown(KeyboardKeys.X))
                    dc.TranslateBackward(0.02f);
            }
        }

        protected override void GameDraw(GameTime gameTime)
        {
            Line.Draw(tr.Position, tr.Position + tr.Up * 2, Color.White);
            Line.Draw(tr.Position, tr.Position + tr.Forward * 2, Color.LightBlue);
            Line.Draw(tr.Position, tr.Position + tr.Right * 2, Color.Red);

            cp.Draw(tr.Matrix, ViewMatrix, ProjectionMatrix, Color.Red);

            cp.Draw(tr2.Matrix, ViewMatrix, ProjectionMatrix, Color.Blue);
            cp.Draw(tr3.Matrix, ViewMatrix, ProjectionMatrix, Color.Green);
            cp.Draw(tr4.Matrix, ViewMatrix, ProjectionMatrix, Color.Yellow);
            cp.Draw(tr5.Matrix, ViewMatrix, ProjectionMatrix, Color.Brown);
            cp.Draw(tr6.Matrix, ViewMatrix, ProjectionMatrix, Color.Gold);
            cp.Draw(tr7.Matrix, ViewMatrix, ProjectionMatrix, Color.BurlyWood);
            cp.Draw(tr8.Matrix, ViewMatrix, ProjectionMatrix, Color.Coral);
        }
    }
}
